//
//  PatientChangeAlertView.m
//  CloudCallDemo
//
//  Created by zhang on 2021/11/17.
//

#import "PatientChangeAlertView.h"


@interface SelectedPatientTableViewCell : UITableViewCell

/**头像*/
@property (nonatomic, strong)UIImageView *headerImage;
@property (nonatomic, strong)UILabel *nameLabel;
@property (nonatomic, strong)UILabel *ageLabel;
@property (nonatomic, strong)UILabel *codeLabel;
@property (nonatomic, strong)UIView *currentView;

/**model*/
@property (nonatomic, strong)LCPatientModel *model;

@end

@implementation SelectedPatientTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpUI];
    }
    return  self;
}


-(void)setUpUI{
    self.headerImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.headerImage];
    self.headerImage.layer.borderWidth = 2;
    self.headerImage.layer.cornerRadius = 22;
    self.headerImage.layer.borderColor = [UIColor colorWithHexString:@""].CGColor;
    self.headerImage.layer.masksToBounds = YES;
    [self.headerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(15);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:16 weight:(UIFontWeightMedium)];
    self.nameLabel.textColor = [UIColor colorWithHexString:@"#000000" withAlpha:0.9];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerImage.mas_top);
        make.left.equalTo(self.headerImage.mas_right).offset(10);
    }];
    self.currentView = [[UIView alloc] init];
    self.currentView.backgroundColor = [UIColor colorWithHexString:@"#FFF3E5"];
    self.currentView.layer.cornerRadius = 10;
    self.currentView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.currentView];
    [self.currentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel.mas_centerY);
        make.left.equalTo(self.nameLabel.mas_right).offset(5);
        make.height.mas_equalTo(20);
    }];
    UILabel *label = [[UILabel alloc] init];
    label.text = @"当前";
    label.textColor = [UIColor colorWithHexString:@"#FF8A00"];
    label.font = [UIFont systemFontOfSize:12];
    [self.currentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(8);
        make.right.offset(-8);
    }];
    self.ageLabel = [[UILabel alloc] init];
    self.ageLabel.font = [UIFont systemFontOfSize:14];
    self.ageLabel.textColor = [UIColor colorWithHexString:@"#000000" withAlpha:0.5];
    [self.contentView addSubview:self.ageLabel];
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(4);
        make.left.equalTo(self.headerImage.mas_right).offset(10);
    }];
    
    self.codeLabel = [[UILabel alloc] init];
    self.codeLabel.font = [UIFont systemFontOfSize:14];
    self.codeLabel.textColor = [UIColor colorWithHexString:@"#000000" withAlpha:0.5];
    [self.contentView addSubview:self.codeLabel];
    [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ageLabel.mas_bottom).offset(4);
        make.left.equalTo(self.headerImage.mas_right).offset(10);
    }];
}


-(void)setModel:(LCPatientModel *)model {
    _model = model;
    self.nameLabel.text = model.name;
    UIImage *holder = [UIImage imageNamed:@"ImageSource.bundle/user_default@3x"];
    UIColor *fromColor = [UIColor colorWithHexString:@"#BFBFBF"];
    if ([model.gender isEqualToString:@"M"]) {
        fromColor = [UIColor colorWithHexString:@"#3D8AE4"];
        holder = [UIImage imageNamed:@"ImageSource.bundle/user_man@3x"];
    } else if ([model.gender isEqualToString:@"F"]) {
        fromColor = [UIColor colorWithHexString:@"#FF55B8"];
        holder = [UIImage imageNamed:@"ImageSource.bundle/user_woman@3x"];
        
    }
    self.headerImage.layer.borderColor = fromColor.CGColor;
    [self.headerImage sd_setImageWithURL:[NSURL URLWithString:model.avatarUrl] placeholderImage:holder];
    NSString *age = @"";
    if (model.age > 0) {
        age = [NSString stringWithFormat:@"%ld",model.age];
    }
    if (model.birthDate) {
        if (model.age > 0) {
            age = [NSString stringWithFormat:@"%@（%@）",age,model.birthDate];
        } else {
            age = model.birthDate;
        }
    }
    self.ageLabel.text = age;
    self.codeLabel.text = [NSString stringWithFormat:@"病历号：%@",model.emrNo?:@""];
    self.currentView.hidden = !model.isSelected;
}

@end



@interface PatientChangeAlertView ()<AlertContentView,UITableViewDelegate,UITableViewDataSource>


/**tableview*/
@property (nonatomic, strong)UITableView *mainTableView;

@end


@implementation PatientChangeAlertView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, 440+AutoBottomHeight)];
    if (self) {
        [self addSubviews];
    }
    return self;
}

-(void)addSubviews{
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor whiteColor];
    UIView *topView = [[UIView alloc] init];
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 50));
    }];
    UILabel *noteLabel = [[UILabel alloc] init];
    noteLabel.textColor = [UIColor colorWithHexString:@"#1D222A"];
    noteLabel.textAlignment = NSTextAlignmentCenter;
    noteLabel.text = @"切换客户";
    noteLabel.font = [UIFont systemFontOfSize:16 weight:(UIFontWeightMedium)];
    [topView addSubview:noteLabel];
    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    UIButton *closeBtn = [[UIButton alloc] init];
    [closeBtn setImage:[UIImage imageNamed:@"ImageSource.bundle/selected_clos@3x"] forState:(UIControlStateNormal)];
    [topView addSubview:closeBtn];
    [closeBtn addTarget:self action:@selector(hiddenAction) forControlEvents:(UIControlEventTouchUpInside)];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.offset(0);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
    [self addSubview:self.mainTableView];
    [self.mainTableView registerClass:[SelectedPatientTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.mainTableView.delegate= self;
    self.mainTableView.dataSource = self;
    self.mainTableView.separatorColor = [UIColor colorWithHexString:@"#EEEEEE"];
    self.mainTableView.tableFooterView = [[UIView alloc] init];
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(topView.mas_bottom).offset(0);
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectedPatientTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    LCPatientModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 95;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    for (LCPatientModel *model in self.dataArray) {
        model.isSelected = NO;
    }
    LCPatientModel *content = self.dataArray[indexPath.row];
    [self hiddenAction];
    if (self.selectedBlock) {
        self.selectedBlock(content);
    }
}

-(void)hiddenAction{
    [self.container hideWithCompetion:nil animated:YES];
}


-(void)showSelectedView{
    [[[AlertView alloc] initWithContent:self] show];
}


@synthesize container;



@end
